package com.ruoyi.utils;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.json.JSONUtil;

public class InvoiceCacheUtil {

    private static Cache<String, String> cache = CacheUtil.newLFUCache(1000);

    public static void put(String key, Object data, long expireTimeSecond) {
        cache.put(key, data instanceof String ? String.valueOf(data) : JSONUtil.toJsonStr(data), DateUnit.SECOND.getMillis() * expireTimeSecond);
    }

    public static <T> T get(String key, Class<T> clazz) {
        String data = cache.get(key);
        if (data == null) {
            return null;
        }
        return clazz == String.class ? (T) data : JSONUtil.toBean(data, clazz);
    }

    public static void delete(String key) {
        cache.remove(key);
    }


}
